package com.fyy.sentinel.controller;

import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author fyy
 * @Description 系统自定义保护实现
 * @Date 2021/7/15 10:08
 */
@RestController
public class SysController {

    @SentinelResource(entryType = EntryType.IN)
    @RequestMapping("/testSys")
    public String hello() throws InterruptedException {
        Thread.sleep(1000);
        return "hello,Sentinel";
    }

    //定义系统自适应规则
    @PostConstruct
    public void initSysRule() {
        //创建存放系统自适应规则的集合
        List<SystemRule> rules = new ArrayList<>();
        //创建系统自适应规则
        SystemRule rule = new SystemRule();
        //定义资源名
        rule.setQps(2);
        //加入集合
        rules.add(rule);
        //加载熔断降级规则
        SystemRuleManager.loadRules(rules);
    }

}
